#include <iostream>
#include <unistd.h>
#include <cstdlib>
#include <string>
#include <sys/wait.h>
using namespace std;
int main() {
    // 子进程创建
    pid_t pid = fork();

    if (pid < 0) {
        std::cerr << "Fork failed!" << std::endl;
        return 1;
    } else if (pid == 0) {
        // 子进程执行execle
        const char *envp[] = {"MY_VAR=HelloWorld", NULL};
        std::cout << "Child process: executing execle..." << std::endl;
        execle("/bin/echo", "echo", "This is a test", NULL, envp);

        // 如果execle失败
        std::cerr << "execle failed!" << std::endl;
        return 1;
    } else {
        // 父进程等待子进程完成
        wait(NULL);
        std::cout << "Parent process: child process finished." << std::endl;
    }

    return 0;
}